package com.threeonemeeting.activity;

import java.util.Map;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;

import com.snda.lib.http.HttpUtil;
import com.snda.lib.http.ICallBack;
import com.threeonemeeting.DataCenter;
import com.threeonemeeting.R;
import com.threeonemeeting.adapter.CityNameListAdapter;
import com.threeonemeeting.adapter.MeetingTradesInfoListAdapter;
import com.threeonemeeting.list.CityInfoList;
import com.threeonemeeting.list.TradesInfoList;
import com.threeonemeeting.listview.CornerListView;
import com.threeonemeeting.model.CityInfo;
import com.threeonemeeting.model.MeetingTradesInfo;
import com.threeonemeeting.tab.BaseTabActivity;
import com.threeonemeeting.task.HttpConst;
import com.threeonemeeting.util.MiscUtil;

public class MainTradesActivity extends BaseTabActivity implements
		OnItemClickListener, ICallBack {

	private CornerListView listViewCategory = null;
	private MeetingTradesInfoListAdapter mTradesAdapter = null;

	private CornerListView listViewCitys = null;
	private CityNameListAdapter mCitysAdapter = null;

	private LinearLayout loadingLayout = null;
	private LinearLayout tradesListLayout = null;
	private LinearLayout citysListLayout = null;

	private Button btnTrades = null;
	private Button btnCitys = null;

	private void initCtrlEvent() {

		loadingLayout = (LinearLayout) findViewById(R.id.loading_layout);
		tradesListLayout = (LinearLayout) findViewById(R.id.trades_list_layout);
		citysListLayout = (LinearLayout) findViewById(R.id.citys_list_layout);

		btnTrades = (Button) findViewById(R.id.btn_hot_trades);
		btnCitys = (Button) findViewById(R.id.btn_hot_city);
		btnTrades.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				onTabTrades();
			}
		});

		btnCitys.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				onTabCitys();
			}
		});

		final EditText editSearch = (EditText) findViewById(R.id.editSearch);
		editSearch.setOnFocusChangeListener(new OnFocusChangeListener() {

			public void onFocusChange(View v, boolean hasFocus) {
				String strHint = getResources().getString(
						R.string.string_search_meeting);
				String strSearch = editSearch.getText().toString();
				if (hasFocus == true) {
					if (strSearch.equalsIgnoreCase(strHint) == true) {
						editSearch.setText("");
					}
				} else {

					if (strSearch.equalsIgnoreCase("") == true) {
						editSearch.setText(strHint);
					}
				}
			}
		});

		// 搜索按钮
		Button btnSearch = (Button) findViewById(R.id.btn_search);
		OnClickListener searchBtnClickListener = new OnClickListener() {

			public void onClick(View v) {
				if (editSearch.getText().toString().equalsIgnoreCase("") == true) {
					return;
				}

				Bundle b = new Bundle();
				b.putString("keyword", editSearch.getText().toString());
				b.putString("title", "搜索：" + editSearch.getText().toString());
				startActivity("SearchMeetingListActivity", b);
			}
		};
		if (btnSearch != null) {
			btnSearch.setOnClickListener(searchBtnClickListener);
		}

	}

	private void initListView() {

		// 行业列表
		mTradesAdapter = new MeetingTradesInfoListAdapter(this);
		listViewCategory = (CornerListView) findViewById(R.id.lv_meeting_category);
		listViewCategory.setAdapter(mTradesAdapter);
		listViewCategory.setOnItemClickListener(this);
		listViewCategory.setCacheColorHint(Color.TRANSPARENT);

		// 城市列表
		mCitysAdapter = new CityNameListAdapter(this);
		mCitysAdapter.setCitysList(DataCenter.getInstance().listCity);
		listViewCitys = (CornerListView) findViewById(R.id.lv_meeting_citys);
		listViewCitys.setAdapter(mCitysAdapter);
		listViewCitys.setOnItemClickListener(this);
		listViewCitys.setCacheColorHint(Color.TRANSPARENT);
		MiscUtil.setListViewHeight(listViewCitys);
	}

	private void onTabTrades() {
		btnTrades.setTextColor(0xFF000000);
		btnCitys.setTextColor(0xFFDFDFDF);

		loadingLayout.setVisibility(View.GONE);
		tradesListLayout.setVisibility(View.VISIBLE);
		citysListLayout.setVisibility(View.GONE);

		ScrollView sv = (ScrollView) findViewById(R.id.scroll_view);
		sv.scrollTo(0, 0);
	}

	private void onTabCitys() {
		btnTrades.setTextColor(0xFFDFDFDF);
		btnCitys.setTextColor(0xFF000000);

		loadingLayout.setVisibility(View.VISIBLE);
		tradesListLayout.setVisibility(View.GONE);
		citysListLayout.setVisibility(View.GONE);

		// 城市
		boolean bRet = MiscUtil.checkCity(getApplicationContext(), this);
		if (bRet == true) {
			CityInfoList listCity = DataCenter.getInstance().listCity;
			if (listCity.size() > 0) {
				loadingLayout.setVisibility(View.GONE);
				tradesListLayout.setVisibility(View.GONE);
				citysListLayout.setVisibility(View.VISIBLE);

				mCitysAdapter.setCitysList(listCity);
				MiscUtil.setListViewHeight(listViewCitys);
			}
		}

		ScrollView sv = (ScrollView) findViewById(R.id.scroll_view);
		sv.scrollTo(0, 0);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_trades);
		initCtrlEvent();
		initListView();

		loadingLayout.setVisibility(View.VISIBLE);
		tradesListLayout.setVisibility(View.GONE);
		citysListLayout.setVisibility(View.GONE);

		boolean bRet = MiscUtil.checkTradesInfo(getApplicationContext(), this);
		if (bRet == true) {
			TradesInfoList listTrades = DataCenter.getInstance().listTrades;
			if (listTrades.size() > 0) {

				if (listTrades.getTradesId("所有行业") == -1) {
					MeetingTradesInfo info = new MeetingTradesInfo();
					info.nCategoryId = 0;
					info.strCategoryName = "所有行业";
					listTrades.insertMeetingTradesInfo(info);
					mTradesAdapter.setTradesList(listTrades);
				}

				mTradesAdapter.setTradesList(listTrades);
				listViewCategory.setAdapter(mTradesAdapter);
				MiscUtil.setListViewHeight(listViewCategory);

				loadingLayout.setVisibility(View.GONE);
				tradesListLayout.setVisibility(View.VISIBLE);
				citysListLayout.setVisibility(View.GONE);

				return;
			}
		}

		// 城市
		bRet = MiscUtil.checkCity(getApplicationContext(), this);
		if (bRet == true) {
			CityInfoList listCity = DataCenter.getInstance().listCity;
			if (listCity.size() > 0) {
				mCitysAdapter.setCitysList(listCity);
				listViewCitys.setAdapter(mCitysAdapter);
				MiscUtil.setListViewHeight(listViewCitys);
			}
		}
	}

	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

		int nIndex = arg2;

		if (arg0 == listViewCategory) {

			MeetingTradesInfo tradesInfo = mTradesAdapter.getItem(nIndex);
			if (tradesInfo == null)
				return;

			Bundle b = new Bundle();
			b.putInt("provinceid", 0);
			b.putInt("cityid", 0);
			b.putInt("type", 0);
			b.putString("keyword", "");

			b.putInt("tradeid", tradesInfo.nCategoryId);
			b.putString("title", tradesInfo.strCategoryName);
			startActivity("SearchMeetingListActivity", b);

		} else if (arg0 == listViewCitys) {

			CityInfo cityInfo = mCitysAdapter.getItem(nIndex);
			if (cityInfo == null)
				return;

			Bundle b = new Bundle();
			b.putInt("tradeid", 0);
			b.putInt("type", 0);
			b.putString("keyword", "");
			b.putInt("cityid", 0);

			if (cityInfo.nProvinceId != 0) {
				b.putInt("provinceid", cityInfo.nProvinceId);
			}

			if (cityInfo.nCityId != 0) {
				b.putInt("cityid", cityInfo.nCityId);
			}

			b.putString("title", cityInfo.strCityName);
			startActivity("SearchMeetingListActivity", b);
		}

	}

	public void doCallBack(Map<String, Object> map) {

		Integer msgType = (Integer) map.get(HttpConst.KEY_TASK_TYPE);
		if (msgType == null) {
			return;
		}

		Integer nError = (Integer) map.get(HttpUtil.KEY_ERROR_CODE);
		if (nError == null || nError != HttpUtil.ERROR_SUCCESS) {
			return;
		}

		// 获取热门的会议列表
		if (msgType == HttpConst.TASKTYPE_GETMEETINGTRADES) {

			mTradesAdapter.clear();

			loadingLayout.setVisibility(View.GONE);
			tradesListLayout.setVisibility(View.VISIBLE);
			citysListLayout.setVisibility(View.GONE);

			TradesInfoList listTrades = DataCenter.getInstance().listTrades;

			MeetingTradesInfo info = new MeetingTradesInfo();
			info.nCategoryId = 0;
			info.strCategoryName = "所有行业";
			listTrades.insertMeetingTradesInfo(info);
			mTradesAdapter.setTradesList(listTrades);

			MiscUtil.setListViewHeight(listViewCategory);
			mTradesAdapter.notifyDataSetChanged();
		} else if (msgType == HttpConst.TASKTYPE_GETCITYLISTS) {

			loadingLayout.setVisibility(View.GONE);
			tradesListLayout.setVisibility(View.GONE);
			citysListLayout.setVisibility(View.VISIBLE);

			mCitysAdapter.setCitysList(DataCenter.getInstance().listCity);
			MiscUtil.setListViewHeight(listViewCitys);
			mTradesAdapter.notifyDataSetChanged();
		}
	}

}
